package cn.edu.ujn.lab3.web;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.service.BaseDictService;
import cn.edu.ujn.lab3.util.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/baseDict")
public class BaseDictController {
	
    @Autowired
    private BaseDictService baseDictService;
    
    //列表查询字典
    @RequestMapping("/list.action")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer rows,
                       BaseDict baseDict, Model model) {
        baseDict.setRows(rows);
        Page<BaseDict> baseDictList = baseDictService.selectBaseDictList(page, baseDict);
        List<BaseDict> allTypeCodeAndTypeName = baseDictService.selectAllTypeCodeAndTypeName();
        model.addAttribute("page", baseDictList);
        model.addAttribute("allTypeCodeAndTypeName", allTypeCodeAndTypeName);
        model.addAttribute("dict_type_code", baseDict.getDict_type_code());
        model.addAttribute("dict_item_name", baseDict.getDict_item_name());
        model.addAttribute("dict_enable", baseDict.getDict_enable());
        return "baseDict";
    }
    
    //创建字典
    @RequestMapping("/create.action")
    @ResponseBody
    public String baseDictCreate(BaseDict baseDict) {
        int rows = baseDictService.createBaseDict(baseDict);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
    
    //根据id查询字典条目
    @RequestMapping("/getBaseDictById.action")
    @ResponseBody
    public BaseDict getBaseDictById(Integer id) {
        return baseDictService.getBaseDictById(id);
    }
    
    //更新字典条目
    @RequestMapping("/update.action")
    @ResponseBody
    public String baseDictUpdate(BaseDict baseDict) {
        int rows = baseDictService.updateBaseDict(baseDict);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
    
    //删除字典条目
    @RequestMapping("/delete.action")
    @ResponseBody
    public String baseDictDelete(Integer id) {
        int rows = baseDictService.deleteBaseDict(id);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
 
}